package com.park.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.park.client.model.article.ArticleModel;

public class GetArticle {
	private static final Logger logger = LoggerFactory.getLogger(GetArticle.class);

	private GetArticle(){}
	
	public static List<ArticleModel> getListFromCSDN(String url){
		List<ArticleModel> list = new ArrayList<>();
		try {
			//博客主页的文章
			Document  document = Jsoup.connect(url).post();
			//列表父div
			Element element =  	document.select(".article-list").get(0);
			Elements ems = element.select(".article-item-box.csdn-tracking-statistics > h4").select("a");
			ArticleModel article = null;
			for (int i = 0; i < ems.size(); i++) {
				if(i == 0){
					continue;
				}
				Element em = ems.get(i);
				String href = em.attr("href");
				Document  document2 = Jsoup.connect(href).post();
				Elements es = document2.select(".htmledit_views");
				article = new ArticleModel();
				article.setArticleTypeId(1L);
				article.setCategoryId(1L);
				article.setCreater(2L);
				article.setArticleTitle(em.text().substring(2));
				article.setArticleBriefIntroduction(es.toString().substring(0,20));
				article.setArticleContent(es.toString());
				list.add(article);
			}
			return list; 
		} catch (IOException e) {
			logger.error("获取文章出错",e);
		}
		return list;
	}
	
	public static void main(String[] args) {
		getListFromCSDN("https://blog.csdn.net/zzh_meng520?t=1");
	}
}
